let activity_tag = 'jd_award'

let store_key = activity_tag+'_today_scores'
let store_notify_key = activity_tag+'_today_scores_notify'
let notify_title = activity_tag+'统计'
let count_key = activity_tag+"_log"


let ips = $store.get('ips');
let todayScores = []
let todayScoresStr = ''
//getTodayScore(ips[0])
for (let k = 0; k < ips.length; k++) {
  let ipItem =  ips[k] 
  getTodayScore(ipItem)
} 
setTimeout(todayScoresDel,5000);

function todayScoresDel() {
$store.set(store_key, todayScores)  

for (let j = 0; j < todayScores.length; j++) {
  let  todayScore =  todayScores[j] 
  todayScoresStr += todayScore+'\n'
} 
//console.log(todayScoresStr)
$store.set(store_notify_key, todayScoresStr)  
pushplus(notify_title, todayScoresStr, '',$store.get('push_key')) 

}


function resultDel(ipUrl,resp) {
  console.log(ipUrl)
  console.log(resp.value)
  todayScores.push(resp.value);
  
}



function getTodayScore(ipItem) {
  // 其他自定义通知
//   console.notify('自行编写函数实现其他比较复杂的通知', '通知标题:', title, '通知内容:', body, url)
let ipUrl = 'http://'+ipItem["ip"]+'/store/'+count_key+'?token='+ipItem["token"]

  let req = {
      url: ipUrl,
      method: 'get',
  
    }
  $axios(req).then(res=>{
    resultDel(ipUrl,res.data)
  }).catch(e=>{
    getTodayScoreOld(ipItem) 
  })
}
function getTodayScoreOld(ipItem) {
  // 其他自定义通知
//   console.notify('自行编写函数实现其他比较复杂的通知', '通知标题:', title, '通知内容:', body, url)
let ipUrl = 'http://'+ipItem["ip"]+'/store?key='+count_key+'&token='+ipItem["token"]

  let req = {
      url: ipUrl,
      method: 'get',
  
    }
  $axios(req).then(res=>{
    resultDel(ipUrl,res.data)
  }).catch(e=>{
    console.log(ipUrl)
    console.error('getTodayScore 失败', e.message)
  })
}



function pushplus(title, body, url,push_key_item) {
  // 其他自定义通知
//   console.notify('自行编写函数实现其他比较复杂的通知', '通知标题:', title, '通知内容:', body, url)
  
  let req = {
      url: 'http://pushplus.hxtrip.com/send',
      headers: {
        'Content-Type': 'application/json; charset=UTF-8'
      },
      method: 'post',
      data: {
        "token": push_key_item,
        "template": "html",
        "title": `${title}`,
        "content": `${body.replace(/\r?\n/g, '<br />')}`
      }
    }
  $axios(req).then(res=>{
    console.log('pushplus 通知结果', res.data)
  }).catch(e=>{
    
    console.error('pushplus 通知失败', e.message)
  })
}